package com.tenc.controller.contacts;

import com.tenc.bean.contacts.Staff;
import com.tenc.feign.contacts.ContactsService;
import com.tenc.utils.CommonUtils;
import com.tenc.pojo.ErrorJson;
import com.tenc.utils.RedisUtil;
import com.tenc.vo.ApiRest;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

/**
 * @author Alan
 * @since 2021/7/20
 */
@RestController
@RequestMapping("/contacts")
@Api("成员管理")
public class ContactsController {

    @Autowired
    ContactsService contacts;

    @ApiOperation("userId转openId")
    @PostMapping("/toOpenid")
    public ApiRest convertOpenId(@RequestParam String userid) {
        Map<String,String> params = new HashMap<>();
        params.put("userid",userid);
        ErrorJson json = contacts.convertOpenId(CommonUtils.get(),params);
        CommonUtils.express(json);
       return ApiRest.ok(json.getOpenid());
    }

    @ApiOperation("创建成员")
    @PostMapping("/create")
    public ApiRest create(@RequestBody Staff staff){
        //TODO 创建系统用户
        ErrorJson json = contacts.createStaff(CommonUtils.get(), staff);
        CommonUtils.express(json);
        return ApiRest.ok();
    }

    @ApiOperation("查看成员详情")
    @GetMapping("/query")
    public ApiRest queryStaff(@RequestParam Map<String,String> map){
        map.put("access_token", RedisUtil.get("acToken"));
        Staff staff = contacts.query(map);
        CommonUtils.baseExpress(staff);
        return ApiRest.result("staff",staff);
    }

    @ApiOperation("更新成员信息")
    @PostMapping("/update")
    public ApiRest update(@RequestBody Staff staff){
        ErrorJson json = contacts.updateStaff(CommonUtils.get(), staff);
        CommonUtils.express(json);
        return ApiRest.ok();
    }

    @ApiOperation("删除某个成员")
    @GetMapping("/delete")
    public ApiRest delete(@RequestParam Map<String,String> map){
        map.put("access_token", RedisUtil.get("acToken"));
        ErrorJson json = contacts.deleteStaff(map);
        CommonUtils.express(json);
        return ApiRest.ok();
    }

    @ApiOperation("批量删除成员")
    @PostMapping("/batch")
    public ApiRest batchDelete(@RequestBody String [] userid){
        ErrorJson json = contacts.batchDeleteStaff(CommonUtils.get(), userid);
        CommonUtils.express(json);
        return ApiRest.ok();
    }

    @ApiOperation("获取部门成员")
    @GetMapping("/list")
    public ApiRest listStaff(@RequestParam Map<String,String> map){
        map.put("access_token", RedisUtil.get("acToken"));
        ErrorJson json = contacts.simpleListStaff(map);
        CommonUtils.express(json);
        return ApiRest.result("list",json.getUserlist());
    }

    @ApiOperation("获取部门成员详情")
    @GetMapping("/list/detail")
    public ApiRest listStaffDetail(@RequestParam Map<String,String> map){
        map.put("access_token", RedisUtil.get("acToken"));
        ErrorJson json = contacts.simpleListStaffDetail(map);
        CommonUtils.express(json);
        return ApiRest.result("list",json.getUserlist());
    }

    @ApiOperation("openId转userId")
    @PostMapping("/toUserId")
    public ApiRest convertUserId(@RequestBody Map<String,String> map){
        ErrorJson json = contacts.convertUserId(CommonUtils.get(), map);
        CommonUtils.express(json);
        return ApiRest.ok(json.getUserid());
    }

    @ApiOperation("设置成员二次验证")
    @GetMapping("/auth")
    public ApiRest auth(@RequestParam Map<String,String> map){
        map.put("access_token", RedisUtil.get("acToken"));
        ErrorJson json = contacts.authSuccessStaff(map);
        CommonUtils.express(json);
        return ApiRest.ok();
    }

    @ApiOperation("邀请成员")
    @PostMapping("/invite")
    public ApiRest invite(@RequestBody Map<String,String> map){
        ErrorJson json = contacts.batchInvite(CommonUtils.get(), map);
        CommonUtils.express(json);
        switch (map.get("type")){
            case "staff" :
                return ApiRest.result("invalid",json.getInvaliduser());
            case "dept" :
                return ApiRest.result("invalid",json.getInvalidparty());
            case "tag" :
                return ApiRest.result("invalid",json.getInvalidtag());
            default:
                return ApiRest.ok();
        }
    }

    @ApiOperation("获取二维码")
    @GetMapping("/qrcode")
    public ApiRest qrcode(@RequestParam Map<String,String> map){
        map.put("access_token", RedisUtil.get("acToken"));
        ErrorJson json = contacts.joinQrcode(map);
        CommonUtils.express(json);
        return ApiRest.ok(json.getJoin_qrcode());
    }

    @ApiOperation("获取某天活跃用户")
    @PostMapping("/active")
    public ApiRest active(@RequestBody Map<String,String> map){
        ErrorJson json = contacts.activeStaff(CommonUtils.get(), map);
        CommonUtils.express(json);
        return ApiRest.result("active",json.getActive_cnt());
    }


}
